<?php
class ProductController extends BaseController{
	public function getCreateProduct() {
		
		$cate = CateProduct::lists('title','id');
		return View::make('backend.product.create')->with('title','Thêm sản phẩm mới')->with('cate',$cate);
	}

	public function postCreateProduct() {
		$valid = Validator::make(Input::all(),Product::$product_rules,Product::$product_langs);

		if ($valid->passes()) {
			$img = Input::file('img');

			if (isset($img)) {
				$upload = $img->move('uploads/products',$img->getClientOriginalName());
				$url = URL::to("/")."/uploads/products/".$img->getClientOriginalName();
			}else{
				$url = '';
			}
			

			$dataInsert = array(
				"title"			=>	Input::get('title'),
				"description"	=>	Input::get('description'),
				"content"		=>	Input::get('content'),
				"price"			=>	Input::get('price'),
				"cateproductID"	=>	Input::get('cate'),
				"userID"		=>	Sentry::getUser()->id,
				"img"			=>	$url,
			);
			
			$data = Product::create($dataInsert);
			
			
			return Redirect::route("list_product")->with('success','Thêm sản phẩm thành công');
		} else {
			return Redirect::route('create_product_get')->with('error',$valid->errors()->first());
		}
	}

	public function listProduct() {
		$search = Input::get('search');
		$product = Product::with('cateproducts','users')->orderBy('id')->where('title',"like","%$search%")->paginate(4);

		return View::make('backend.product.list')->with('title','Danh sách sản phẩm')->with("products",$product)->with('search',$search);
	}

	public function deleteProduct($id) {
		$product = Product::find($id);

		if ($product) {

			$product->delete();
			return Redirect::route('list_product')->with('error','Xóa sản phẩm thành công');

		}else{
			return Redirect::route('list_product')->with('error','Không tồn tại sản phẩm này');
		}
	}
	public function editProductget($id) {

		$cate = CateProduct::lists('title','id');

		$product = Product::find($id);

		if ($product){
			return View::make('backend.product.edit')->with('title','Sửa sản phẩm')->with('products',$product)->with('cate',$cate);
		} else {
			return Redirect::route('list_product')->with('error','Không tồn tại sản phẩm này');
		}

	}
	public function editProductpost() {
			$products = Product::all()->toArray();

			foreach ($products as $product) {
				$img = Input::file('img');

				if (isset($posts['img'])) {
					$upload = $img->move("uploads",$img->getClientOriginalName());
					$url = URL::to("/")."/uploads/".$img->getClientOriginalName();
				} else {
					$url = $product['img'];
				}
			}
			
			$product_model = new Product();
			$id = Input::get('product_id');
			
				
			$product_model = Product::find($id);
			$product_model->title = Input::get('title');
			$product_model->description = Input::get('description');
			$product_model->content = Input::get('content');
			$product_model->cateproductID = Input::get('cate');
			$product_model->userID = Sentry::getUser()->id;
			$product_model->price = Input::get('price');
			$product_model->img = $url;
		 	$product_model->save();
			
			return Redirect::route("list_product")->with("success","Sửa sản phẩm thành công");

	}
}